/**
 * 
 */
package GUI;

import javax.swing.JPanel;
import java.awt.*;

import javax.swing.*;

import Entity.Customer;
import Entity.User;
import Utils.MailValidator;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/**
 * @author Nir
 *
 */
public class MailToCustomer extends JPanel {
	
	private static final long serialVersionUID = 1L;

	private JButton btnBack;
	private JTextField lNametextField;

	private JLabel lblGLastName;
	private JLabel lblBFirstName;
	private JLabel lblBLastName;
	private JLabel lblBPhone;
	private JTextField bPhogeTextField;
	private JButton btnSend;
	private JComboBox<String> bLnamecomboBox;
	

	private JLabel iconlabel;
	private JLabel lblGFirstName;
	private JLabel lblCustomerId;
	private JComboBox<String> CustomerIDcomboBox;
	private JLabel lblGroomPhone;
	private JLabel lblAddress;
	private JTextField gPonetextField;
	private JComboBox<String> gFnameComboBox;
	private JComboBox<String> gLnameComboBox;
	private JComboBox<String> bFnameComboBox;
	private JButton btnRefresh;
	
	private ArrayList<Customer> c_allDate;
	private ArrayList<Customer> c_filteredDate;
	private Object syncObject;
	private boolean c_isChangeAllowed;
	private JTextField EmailtextField;
	private JComboBox<String> AddresscomboBox;
			
			public MailToCustomer() {
				
				setBackground(new Color(255, 51, 0));
				setLayout(null);
				btnBack = new JButton("Back");
				btnBack.setIcon(new ImageIcon(ViewCustomerDetails.class.getResource("/resources/back22.png")));
				lNametextField = new JTextField();

				lNametextField.setEditable(false);
				EmailtextField = new JTextField();
				EmailtextField.setFont(new Font("Tahoma", Font.BOLD, 12));
				bPhogeTextField = new JTextField();
				bPhogeTextField.setFont(new Font("Tahoma", Font.BOLD, 12));
				bPhogeTextField.setEditable(false);
				gPonetextField = new JTextField();
				gPonetextField.setFont(new Font("Tahoma", Font.BOLD, 12));
				CustomerIDcomboBox = new JComboBox<String>();
				btnRefresh = new JButton("Refresh");
				gFnameComboBox = new JComboBox<String>();
				bFnameComboBox = new JComboBox<String>();
				gLnameComboBox = new JComboBox<String>();
				AddresscomboBox = new JComboBox<String>();
				AddresscomboBox.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						synchronized (syncObject) {
							if(isChangeAllowed()){
								filterList(AddresscomboBox.getSelectedItem().toString(),"ADDRESS");
							}
						}
					}
				});

				initialize();
			}

			private void initialize() {


				JLabel lblCustomerDetails = new JLabel("Finished Mail");
				lblCustomerDetails.setForeground(new Color(0, 204, 255));
				lblCustomerDetails.setFont(new Font("Tahoma", Font.BOLD, 16));
				lblCustomerDetails.setBounds(10, 11, 211, 44);
				add(lblCustomerDetails);
				
				btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
				btnBack.setBounds(10, 462, 101, 27);
				add(btnBack);


				
				JLabel labelEmail = new JLabel("Email");
				labelEmail.setForeground(Color.WHITE);
				labelEmail.setFont(new Font("Tahoma", Font.BOLD, 12));
				labelEmail.setBounds(361, 325, 101, 27);
				add(labelEmail);
				

				lblGLastName = new JLabel("Groom Last Name");
				lblGLastName.setForeground(Color.WHITE);
				lblGLastName.setFont(new Font("Tahoma", Font.BOLD, 12));
				lblGLastName.setBounds(10, 207, 111, 22);
				add(lblGLastName);
				
				lblBFirstName = new JLabel("Bride First Name");
				lblBFirstName.setForeground(Color.WHITE);
				lblBFirstName.setFont(new Font("Tahoma", Font.BOLD, 12));
				lblBFirstName.setBounds(10, 268, 111, 22);
				add(lblBFirstName);
				
				lblBLastName = new JLabel("Bride Last Name");
				lblBLastName.setForeground(Color.WHITE);
				lblBLastName.setFont(new Font("Tahoma", Font.BOLD, 12));
				lblBLastName.setBounds(10, 325, 101, 22);
				add(lblBLastName);
				
				lblBPhone = new JLabel("Bride Phone");
				lblBPhone.setForeground(Color.WHITE);
				lblBPhone.setFont(new Font("Tahoma", Font.BOLD, 12));
				lblBPhone.setBounds(361, 274, 82, 22);
				add(lblBPhone);
				
				
				bPhogeTextField.setColumns(10);
				bPhogeTextField.setBounds(499, 270, 135, 27);
				add(bPhogeTextField);

				ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
				ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
				
				lblGFirstName = new JLabel("Groom First Name");
				lblGFirstName.setFont(new Font("Tahoma", Font.BOLD, 12));
				lblGFirstName.setForeground(Color.WHITE);
				lblGFirstName.setBounds(10, 151, 127, 26);
				add(lblGFirstName);
				
				lblCustomerId = new JLabel("Customer ID");
				lblCustomerId.setForeground(Color.WHITE);
				lblCustomerId.setFont(new Font("Tahoma", Font.BOLD, 12));
				lblCustomerId.setBounds(10, 99, 82, 22);
				add(lblCustomerId);
				
				
				CustomerIDcomboBox.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						synchronized (syncObject) {
							if(isChangeAllowed()){
								filterList(CustomerIDcomboBox.getSelectedItem().toString(),"CUSTOMERID");
							}
						}
					}
				});
				
				CustomerIDcomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
				CustomerIDcomboBox.setBounds(161, 95, 135, 27);
				add(CustomerIDcomboBox);
				
				lblGroomPhone = new JLabel("Groom Phone");
				lblGroomPhone.setForeground(Color.WHITE);
				lblGroomPhone.setFont(new Font("Tahoma", Font.BOLD, 12));
				lblGroomPhone.setBounds(361, 201, 95, 17);
				add(lblGroomPhone);
				
				lblAddress = new JLabel("Address");
				lblAddress.setForeground(Color.WHITE);
				lblAddress.setFont(new Font("Tahoma", Font.BOLD, 12));
				lblAddress.setBounds(361, 100, 82, 21);
				add(lblAddress);
				
				
				gPonetextField.setEditable(false);
				gPonetextField.setBounds(499, 198, 135, 27);
				add(gPonetextField);
				gPonetextField.setColumns(10);
				
				gFnameComboBox.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						synchronized (syncObject) {
							if(isChangeAllowed()){
								filterList(gFnameComboBox.getSelectedItem().toString(),"GFNAME");
							}
						}
					}
				});
				gFnameComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
				gFnameComboBox.setBounds(161, 155, 135, 27);
				add(gFnameComboBox);
				
				
				gLnameComboBox.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						synchronized (syncObject) {
							if(isChangeAllowed()){
								filterList(gLnameComboBox.getSelectedItem().toString(),"GLNAME");
							}
						}
					}
				});
				gLnameComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
				gLnameComboBox.setBounds(161, 209, 135, 27);
				add(gLnameComboBox);
				
				bLnamecomboBox = new JComboBox<String>();
				bLnamecomboBox.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						synchronized (syncObject) {
							if(isChangeAllowed())
								filterList(bLnamecomboBox.getSelectedItem().toString(),"BLNAME");		
						}
					}
				});;
				bLnamecomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
				bLnamecomboBox.setBounds(161, 322, 135, 27);
				add(bLnamecomboBox);
				
				
				bFnameComboBox.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						synchronized (syncObject) {
							if(isChangeAllowed()){
								filterList(bFnameComboBox.getSelectedItem().toString(),"BFNAME");
							}
						}
					}
				});
				bFnameComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
				bFnameComboBox.setBounds(161, 270, 135, 27);
				add(bFnameComboBox);
				
				
				btnRefresh.setIcon(new ImageIcon(ViewCustomerDetails.class.getResource("/resources/arrow-refresh-2-icon.png")));
				btnRefresh.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
				btnRefresh.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						synchronized (syncObject) {
							if(isChangeAllowed()){
								gPonetextField.setText("");
								bPhogeTextField.setText("");
								EmailtextField.setText("");
								filterList(null,"ALL");
							}
						}
					}
				});
				btnRefresh.setBounds(270, 399, 120, 27);
				add(btnRefresh);
				
				
				EmailtextField.setEditable(false);
				EmailtextField.setBounds(499, 326, 150, 27);
				add(EmailtextField);
				EmailtextField.setColumns(10);
				
				
				AddresscomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
				AddresscomboBox.setBounds(499, 101, 135, 51);
				add(AddresscomboBox);
				
				btnSend = new JButton("Send");
				btnSend.setBounds(529, 413, 89, 23);
				add(btnSend);
				
				iconlabel = new JLabel("");
				iconlabel.setIcon(LOGO);
				iconlabel.setBounds(0, 0, 786, 500);
				add(iconlabel);
			}

			public void setAllCustomer(ArrayList<Customer> list){
				c_allDate = list;
				c_filteredDate = new ArrayList<>();
				syncObject = new Object();
				filterList(null,"ALL");
				 
			}
			
			private void filterList(String selectedItem, String scope)
			{
				synchronized (syncObject) {
					c_filteredDate.clear();
					
					switch(scope)
					{
					case "GFNAME":
						for (Customer c :c_allDate) 
						{
							if(c.getgFname().equals(selectedItem))
							{
								c_filteredDate.add(c);
							}
						}
						break;
					case "GLNAME":
						for (Customer c :c_allDate) 
						{
							if(c.getgLname().equals(selectedItem))
							{
								c_filteredDate.add(c);
							}
						}
						break;
					case "BFNAME":
						for (Customer c :c_allDate) 
						{
							if(c.getbFname().equals(selectedItem))
							{
								System.out.println("in bfm");
								c_filteredDate.add(c);
							}
						}
						break;
					case "BLNAME":
						for (Customer c :c_allDate) 
						{
							if(c.getbLname().equals(selectedItem))
							{
								
								c_filteredDate.add(c);
							}
						}
						break;
					case "CUSTOMERID":
						for (Customer c :c_allDate) 
						{
							if(String.valueOf(c.getcID()).equals(selectedItem))
							{
								c_filteredDate.add(c);
							}
						}
						break;
					case "ADDRESS":
						for (Customer c :c_allDate) 
						{
							if(c.getAddress().equals(selectedItem))
							{
								c_filteredDate.add(c);
							}
						}
						break;
					case "ALL":
						c_filteredDate.addAll(c_allDate);
						break;
					}
				}
				c_isChangeAllowed = false;
				filterComboBox(gFnameComboBox,"GFNAME");
				filterComboBox(gLnameComboBox,"GLNAME");
				filterComboBox(bFnameComboBox,"BFNAME");
				filterComboBox(bLnamecomboBox,"BLNAME");
				filterComboBox(CustomerIDcomboBox,"CUSTOMERID");
				filterComboBox(AddresscomboBox,"ADDRESS");
				if(gFnameComboBox.getItemCount()==1 && gLnameComboBox.getItemCount()==1 && bFnameComboBox.getItemCount()==1 && 
						bLnamecomboBox.getItemCount()==1 && CustomerIDcomboBox.getItemCount()==1&&AddresscomboBox.getItemCount()==1){
					setCustDetails();
				}
				c_isChangeAllowed = true;

			}
			/**
			* This method adding items to combobox
			* @param combo is the combobox to add item to
			* @param scope is the key code for adding the correct field 
			* 
			*/
			private void filterComboBox(JComboBox<String> combo, String scope)
			{
				combo.removeAllItems();
				synchronized (syncObject)
				{
					for(Customer c: c_filteredDate )
					{
						switch(scope)
						{
						case "GFNAME":
							combo.addItem(c.getgFname());
							break;
						case "GLNAME":
							combo.addItem(c.getgLname());
							break;
						case "BFNAME":
							System.out.println(c.getbFname());
							combo.addItem(c.getbFname());
							break;
						case "BLNAME":
							combo.addItem(c.getbLname());
							break;
						case "CUSTOMERID":
							combo.addItem(String.valueOf(c.getcID()));
							break;
						case "ADDRESS":
							combo.addItem(c.getAddress());
							break;
						}
					}
				}
			}
			/**
			* @return the e_isChangeAllowed
			* 
			*/
			private boolean isChangeAllowed()
			{
				return c_isChangeAllowed;
			}

			/**
			 * This method add the customers details
			 */
			public void setCustDetails(){
				
				for(Customer c : c_filteredDate){
					
					if(c.getcID()==Integer.parseInt(CustomerIDcomboBox.getSelectedItem().toString()))
					{
						gPonetextField.setText(c.getgPhone());
						bPhogeTextField.setText(c.getbPhone());
						EmailtextField.setText(c.getEmail());
					}
					break;
				}
			}
			/**
			 * @return the gPonetextField
			 */
			public JTextField getgPonetextField() {
				return gPonetextField;
			}

			/**
			 * @param gPonetextField the gPonetextField to set
			 */
			public void setgPonetextField(JTextField gPonetextField) {
				this.gPonetextField = gPonetextField;
			}


			/**
			 * @return the emailtextField
			 */
			public JTextField getEmailtextField() {
				return EmailtextField;
			}

			/**
			 * @param emailtextField the emailtextField to set
			 */
			public void setEmailtextField(JTextField emailtextField) {
				EmailtextField = emailtextField;
			}

			/**
			 * @return the customerIDcomboBox
			 */
			public JComboBox<String> getCustomerIDcomboBox() {
				return CustomerIDcomboBox;
			}

			/**
			 * @param customerIDcomboBox the customerIDcomboBox to set
			 */
			public void setCustomerIDcomboBox(JComboBox<String> customerIDcomboBox) {
				CustomerIDcomboBox = customerIDcomboBox;
			}

			/**
			 * @return the bPhogeTextField
			 */
			public JTextField getbPhogeTextField() {
				return bPhogeTextField;
			}

			/**
			 * @param bPhogeTextField the bPhogeTextField to set
			 */
			public void setbPhogeTextField(JTextField bPhogeTextField) {
				this.bPhogeTextField = bPhogeTextField;
			}

			/**
			 * @return the bLnamecomboBox
			 */
			public JComboBox<String> getbLnamecomboBox() {
				return bLnamecomboBox;
			}

			/**
			 * @return the gFnameComboBox
			 */
			public JComboBox<String> getgFnameComboBox() {
				return gFnameComboBox;
			}

			/**
			 * @return the gLnameComboBox
			 */
			public JComboBox<String> getgLnameComboBox() {
				return gLnameComboBox;
			}

			/**
			 * @return the bFnameComboBox
			 */
			public JComboBox<String> getbFnameComboBox() {
				return bFnameComboBox;
			}

			/**
			 * @return the btnBack
			 */
			public JButton getBtnBack() {
				return btnBack;
			}

			/**
			 * @return the btnRefresh
			 */
			public JButton getBtnRefresh() {
				return btnRefresh;
			}

			/**
			 * @param btnRefresh the btnRefresh to set
			 */
			public void setBtnRefresh(JButton btnRefresh) {
				this.btnRefresh = btnRefresh;
			}

			/**
			 * @return the c_allDate
			 */
			public ArrayList<Customer> getC_allDate() {
				return c_allDate;
			}

			/**
			 * @param c_allDate the c_allDate to set
			 */
			public void setC_allDate(ArrayList<Customer> c_allDate) {
				this.c_allDate = c_allDate;
			}

			/**
			 * @return the c_filteredDate
			 */
			public ArrayList<Customer> getC_filteredDate() {
				return c_filteredDate;
			}

			/**
			 * @param c_filteredDate the c_filteredDate to set
			 */
			public void setC_filteredDate(ArrayList<Customer> c_filteredDate) {
				this.c_filteredDate = c_filteredDate;
			}

			/**
			 * @return the c_isChangeAllowed
			 */
			public boolean isC_isChangeAllowed() {
				return c_isChangeAllowed;
			}

			/**
			 * @param c_isChangeAllowed the c_isChangeAllowed to set
			 */
			public void setC_isChangeAllowed(boolean c_isChangeAllowed) {
				this.c_isChangeAllowed = c_isChangeAllowed;
			}

			/**
			 * @param btnBack the btnBack to set
			 */
			public void setBtnBack(JButton btnBack) {
				this.btnBack = btnBack;
			}

			/**
			 * @param bLnamecomboBox the bLnamecomboBox to set
			 */
			public void setbLnamecomboBox(JComboBox<String> bLnamecomboBox) {
				this.bLnamecomboBox = bLnamecomboBox;
			}

			/**
			 * @param gFnameComboBox the gFnameComboBox to set
			 */
			public void setgFnameComboBox(JComboBox<String> gFnameComboBox) {
				this.gFnameComboBox = gFnameComboBox;
			}

			/**
			 * @param gLnameComboBox the gLnameComboBox to set
			 */
			public void setgLnameComboBox(JComboBox<String> gLnameComboBox) {
				this.gLnameComboBox = gLnameComboBox;
			}

			/**
			 * @param bFnameComboBox the bFnameComboBox to set
			 */
			public void setbFnameComboBox(JComboBox<String> bFnameComboBox) {
				this.bFnameComboBox = bFnameComboBox;
			}

			/**
			 * @return the addresscomboBox
			 */
			public JComboBox<String> getAddresscomboBox() {
				return AddresscomboBox;
			}

			/**
			 * @param addresscomboBox the addresscomboBox to set
			 */
			public void setAddresscomboBox(JComboBox<String> addresscomboBox) {
				AddresscomboBox = addresscomboBox;
			}

			/**
			 * @return the btnSend
			 */
			public JButton getBtnSend() {
				return btnSend;
			}
		}

